package com.sanploy.manage.service.impl;

import com.sanploy.common.domain.dto.OrderSort;
import com.sanploy.common.domain.vo.OrderListVo;
import com.sanploy.common.domain.vo.Page;
import com.sanploy.common.enums.Meta;
import com.sanploy.common.enums.OrderShippingStatus;
import com.sanploy.common.enums.OrderStatus;
import com.sanploy.common.util.CommonUtil;
import com.sanploy.common.vo.BaseException;
import com.sanploy.manage.mapper.OrderMapper;
import com.sanploy.manage.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by zhu yingzhi on 2017/10/11.
 * @author yingzhi zhu
 * 订单逻辑处理接口实现
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public Page<OrderListVo> findAllOrder(OrderSort orderSort) {
        Page<OrderListVo> goodsListVoPage = new Page<>();
        int total = orderMapper.findAllOrderCount(orderSort); //查找数量
        orderSort.setStart((orderSort.getCurrentPage()-1)*orderSort.getPageSize());
        goodsListVoPage.setCurrentPage(orderSort.getCurrentPage());
        goodsListVoPage.setPageSize(orderSort.getPageSize());
        goodsListVoPage.setList(orderMapper.findAllOrder(orderSort));
        goodsListVoPage.setTotal(total);
        goodsListVoPage.setTotalPage((int)Math.ceil((double)total/(double)orderSort.getPageSize()));
        return goodsListVoPage;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deliverGoods(String order_id, String[] transport_number) throws Exception {
        CommonUtil.judgeRUD(orderMapper.deliverGoods
                (order_id, OrderStatus.ReceiveGoods.toString(),CommonUtil.getTimeNormal(),OrderStatus.WaitGoods.toString()), Meta.UpdateFail); //确认是否可以发货 并从订单层面发货

        return CommonUtil.judgeRUD( orderMapper.deliverGoodsItem
                (order_id, OrderShippingStatus.Shipped.toString(),CommonUtil.getTimeNormal()), Meta.UpdateFail);//无误后从 订单商品层面发货
    }

    @Override
    public OrderListVo findByOrderId(String order_id) throws BaseException {
        return orderMapper.findByOrderId(order_id);
    }


    @Override
    public int autoChangeOrdersStatus(List<String> list, OrderStatus status,OrderStatus beforeStatus,String endTime) throws BaseException {
        return orderMapper.autoChangeOrdersStatusBat(
                list,status.toString(),CommonUtil.getTimeNormal(),beforeStatus.toString(),endTime);
    }

    @Override
    public int autoChangeOrdersStatus(String order_id, OrderStatus status,OrderStatus beforeStatus,String endTime){
        return orderMapper.autoChangeOrdersStatus(
                order_id,status.toString(),CommonUtil.getTimeNormal(),beforeStatus.toString(),endTime);
    }


    @Override
    public int changeUserOrdersStatus(Long user_id, OrderStatus status, OrderStatus beforeStatus, String endTime) {
        return orderMapper.changeUserOrdersStatus(
                user_id,status.toString(),CommonUtil.getTimeNormal(),beforeStatus.toString(),endTime);
    }

    @Override
    public List<String> findUserOrdersByStatus(Long user_id, OrderStatus status) {
        return orderMapper.findCancerUserOrders(user_id,status.toString());
    }

    @Override
    public List<String> findOrdersByStatus(OrderStatus status) {
        return orderMapper.findOrdersByStatus(status.toString());
    }
}
